import os.path
import sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

import click

from dp_analysis.analysis.base import DrawBase
from dp_analysis.db.connection import Mysql
from dp_analysis.utils.decorate import decorate


class DrawFavoriteDishes(DrawBase):
    def __init__(self):
        super().__init__()

    @decorate(obj_name="DrawFavoriteDishes")
    def analyse(self, db, limit):
        with Mysql("dp", db) as mysql:
            connection = mysql.engine.connect()

            word_dict = dict()
            number = 0
            for (favorite_dished, ) in connection.execute(
                    f"SELECT favorite_dishes  FROM shop_comment WHERE favorite_dishes != '' LIMIT {limit};"):
                dish_list = favorite_dished.split(";")
                if not dish_list:
                    continue
                for dish in dish_list:
                    word_dict.setdefault(dish, 0)
                    word_dict[dish] += 1
                number += 1
                if number % 1000 == 0:
                    print(f"已处理{number}条评论...")
            print(f"共处理评论{number}条，存在菜品{len(word_dict)}种！")

            word_list = list()
            for (word, _count) in word_dict.items():
                word_list.extend([word] * _count)
            # 绘制词云
            self.draw_word_cloud("12大热门地区", "食客最喜欢的菜", word_list, "食客最喜欢的菜", "favorite_dish/")

    __call__ = analyse


@click.command()
@click.option("--db", default=0, help="请选择使用的数据库")
@click.option("--limit", default=10000, help="请选择使用的评论数量")
def main(db, limit):
    draw = DrawFavoriteDishes()
    draw.analyse(db, limit)


if __name__ == '__main__':
    main()
